FROM ros:humble-ros-base

ARG USERNAME=rosdev
ARG UID=1000
ARG GID=$UID


RUN sudo mv /etc/apt/sources.list /etc/apt/sources.list.bak && \
    echo 'deb https://mirrors.aliyun.com/ubuntu-ports/ jammy main restricted universe multiverse'               >> /etc/apt/sources.list && \
    echo 'deb-src https://mirrors.aliyun.com/ubuntu-ports/ jammy main restricted universe multiverse'           >> /etc/apt/sources.list && \
    echo 'deb https://mirrors.aliyun.com/ubuntu-ports/ jammy-security main restricted universe multiverse'      >> /etc/apt/sources.list && \
    echo 'deb-src https://mirrors.aliyun.com/ubuntu-ports/ jammy-security main restricted universe multiverse'  >> /etc/apt/sources.list && \
    echo 'deb https://mirrors.aliyun.com/ubuntu-ports/ jammy-updates main restricted universe multiverse'       >> /etc/apt/sources.list && \
    echo 'deb-src https://mirrors.aliyun.com/ubuntu-ports/ jammy-updates main restricted universe multiverse'   >> /etc/apt/sources.list && \
    echo 'deb https://mirrors.aliyun.com/ubuntu-ports/ jammy-backports main restricted universe multiverse'     >> /etc/apt/sources.list && \
    echo 'deb-src https://mirrors.aliyun.com/ubuntu-ports/ jammy-backports main restricted universe multiverse' >> /etc/apt/sources.list

# 替换为阿里云源并跳过密钥验证
#RUN rm -f /etc/apt/sources.list.d/ros2.sources && \
#    echo "Types: deb" >> /etc/apt/sources.list.d/ros2.sources && \
#    echo "URIs: https://mirrors.aliyun.com/ros2/ubuntu" >> /etc/apt/sources.list.d/ros2.sources && \
#    echo "Suites: $(lsb_release -sc)" >> /etc/apt/sources.list.d/ros2.sources && \
#    echo "Components: main" >> /etc/apt/sources.list.d/ros2.sources && \
#    echo "Trusted: yes" >> /etc/apt/sources.list.d/ros2.sources

# Install some dependencies packages
RUN apt update -q \
    && apt upgrade -q -y \
    && apt install -y --no-install-recommends \
    software-properties-common \
    python3-pip \
    xauth vim curl git wget  \
    && apt clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*


RUN groupadd -g $GID $USERNAME \
    && useradd -lm -u $UID -g $USERNAME -s /bin/bash $USERNAME \
    && chown -R $USERNAME:$USERNAME /home/$USERNAME \
    && echo "$USERNAME ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
USER $USERNAME

# 配置 pip 阿里源
RUN mkdir -p /home/$USERNAME/.pip \
    && echo "[global]\nindex-url = https://mirrors.aliyun.com/pypi/simple/\ntrusted-host = mirrors.aliyun.com" > /home/$USERNAME/.pip/pip.conf

# 安装 Miniconda
ENV CONDA_DIR=/opt/conda
ENV PATH=$CONDA_DIR/bin:$PATH
RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-latest-$(uname -s)-$(uname -m | sed 's/arm64/aarch64/').sh -O /tmp/miniconda.sh
RUN sudo chmod 777 /tmp/miniconda.sh && sudo /tmp/miniconda.sh -b -p $CONDA_DIR \
    && sudo rm /tmp/miniconda.sh

RUN conda create -y -n ros2 python=3.10

# Create workspace so that user own this directory
RUN mkdir -p /home/$USERNAME/ros2_ws/src
WORKDIR /home/$USERNAME/ros2_ws

# Copy configuration files
RUN echo 'source /opt/ros/'$ROS_DISTRO'/setup.bash' >> /home/$USERNAME/.bashrc \
    && echo 'source /home/'$USERNAME'/ros2_ws/install/setup.bash' >> /home/$USERNAME/.bashrc

# Setup entrypoint
COPY ./ros_entrypoint.sh /
ENTRYPOINT ["/ros_entrypoint.sh"]
CMD ["bash"]
